from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from sign.models import Event, Guest
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404
# Create your views here.


def index(request):
    return render(request, "index.html")


'''
def login_action(request):
    if request.method == "POST":
        username = request.POST.get("username", "")
        password = request.POST.get("password", "")
        if username == "root" and password == "123456":
            response = HttpResponseRedirect("/loginsuccess/")
            request.session["user"] = username
            return response
        else:
            return render(request, "index.html", {"error": "用户名或密码错误"})
'''


def login_action(request):
    if request.method == "POST":
        username = request.POST.get("username", "")
        password = request.POST.get("password", "")
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request,user)
            request.session["user"] = username
            response = HttpResponseRedirect("/login_success")
            return response
        else:
            return render(request, "index.html", {"error": "用户名或密码错误"})


@login_required
def login_success(request):
    event_list = Event.objects.all()
    username = request.session.get('user', "")
    return render(request, "login_success.html", {'user': username})


@login_required()
def guest_manage(request):
    username = request.session.get("user","")   #读取浏览器session
    guest_list = Guest.objects.all()    #查询所有嘉宾对象

    paginator = Paginator(guest_list, 15)  # 创建每页2条数据的分页器
    page = request.GET.get('page')  # 通过get请求得到当前要显示的第几页数据
    try:
        contacts = paginator.page(page)  # 获取第page页的数据，如果没有抛出异常
    except PageNotAnInteger:
        # 如果page页不是整数，取第1页数据
        contacts = paginator.page(1)
    except EmptyPage:
        # 如果page不在范围，取最后1页数据
        contacts = paginator.page(paginator.num_pages)
    return render(request, "guest_manage.html", {"user": username, "guests": contacts})
    #return render(request,"guest_manage.html",{"user":username,"guests":guest_list})

# 名称搜索
@login_required
def search_name(request):
    username = request.session.get('user', '')
    search_name = request.GET.get("name", "")
    event_list = Event.objects.filter(name__contains=search_name)
    return render(request, "login_success.html", {"user": username, "events": event_list})


def guest_search_name(request):
    username = request.session.get('user', '')
    guest_search_name = request.GET.get("realname", "")
    guest_list = Guest.objects.filter(realname__contains=guest_search_name)

    paginator = Paginator(guest_list, 2)  # 创建每页2条数据的分页器
    page = request.GET.get('page')  # 通过get请求得到当前要显示的第几页数据
    try:
        contacts = paginator.page(page)  # 获取第page页的数据，如果没有抛出异常
    except PageNotAnInteger:
        # 如果page页不是整数，取第1页数据
        contacts = paginator.page(1)
    except EmptyPage:
        # 如果page不在范围，取最后1页数据
        contacts = paginator.page(paginator.num_pages)
    return render(request, "guest_manage.html", {"user": username, "guests": contacts})


    #签到页面
@login_required
def sign_index(request,eid):
    event = get_object_or_404(Event,id=eid)
    return render(request,"sign_index.html",{"event":event})



@login_required
def sign_index_action(request,eid):
    event = get_object_or_404(Event, id=eid)
    phone = request.POST.get("phone", "")
    print(phone)
    result = Guest.objects.filter(phone=phone)
    if not result:
        return render(request, "sign_index.html", {"event": event, "hint": "手机号错误！"})
        result = Guest.objects.filter(phone=phone, event_id=eid)
    if not result:
        return render(request, "sign_index.html", {"event": event, "hint": "发布会id或者手机号错误！"})
        result = Guest.objects.get(phone=phone, event_id=eid)
        if result.sign:
            return render(request, "sign_index.html", {"event": event, "hint": "用户已签到！"})
        else:
            Guest.objects.filter(phone=phone, event_id=eid).update(sign="1")
            return render(request, "sign_index.html", {"event": event, "hint": "签到成功！", "guest": result})


#退出系统
@login_required
def logout(request):
    auth.logout(request)    #退出登录
    response = HttpResponseRedirect("/index/")
    return response

